package com.ky.serialport.sensor;

import com.ky.serialport.bean.ComBean;
import com.ky.serialport.utils.MyFunc;

import static com.ky.serialport.utils.FormatUtils.df2;
import static com.ky.serialport.utils.RandomUtils.myRandom;

/**
 * 甲烷数据
 * 
 */
public class JiaWanDataBean {

    public int signal; //信号值
    public int power; //电量值
    public long time; //数据包时间
    public int index = -1;
    public int type = -1;
    public ComBean comBean;
    public float jiaWanVel; //甲烷

    public JiaWanDataBean() {
    }

    public JiaWanDataBean(ComBean comBean) {
        caculate(comBean.recData);
        this.comBean = comBean;
        time = comBean.recTime;
    }

    public JiaWanDataBean(byte[] recData) {
        caculate(recData);
        time = System.currentTimeMillis();
    }

    public JiaWanDataBean caculate(ComBean comBean){
        time = comBean.recTime;
        return caculate(comBean.recData);
    }

    public JiaWanDataBean caculate(byte[] buffer) {
        // TODO: 2020/1/3 数据解析
        signal = MyFunc.getSignal(buffer[31]); //测试信号
        power = MyFunc.getPower(buffer[29], buffer[30]);//电量
        index = buffer[10];
        type = buffer[9];
        String jingya = df2.format((float) MyFunc.twoByte2int(buffer, 27) / 100);
        int FuHAo = buffer[26] & 0x00;
        String resJw = "";
        if (FuHAo == 0x00) {
            resJw = jingya;
        } else {
            resJw = "- " + jingya;
        }
        jiaWanVel = Float.parseFloat(resJw);
        return this;
    }

    public JiaWanDataBean test(int index){
        time = System.currentTimeMillis();
        signal = 200;
        power = 300 + myRandom.nextInt(120);
        jiaWanVel = myRandom.nextFloat();
        return this;
    }

}
